#include "application.h"
#include "mainwindow.h"
#include <Core>
#include <StateManager>

#include <ToolCategory>
#include <ToolManager>
#include "src/tools/toolselect.h"
#include "src/tools/toolmove.h"
#include "src/tools/toolrotate.h"
#include "src/tools/toolcreateline.h"

Application::Application(int &argc, char **argv) :
    QApplication(argc, argv)
{
}

Application &Application::app()
{
    Application* pApp = dynamic_cast<Application*>(QCoreApplication::instance());
    Q_ASSERT(pApp);
    return *pApp;
}

bool Application::initInstance()
{
    Core::instance().init();

    m_undoGroup = new QUndoGroup(this);
    m_mainWindow = new MainWindow;

    initTools();

    m_mainWindow->show();

    return true;
}

void Application::exitInstance()
{
}

QPointer<Document> Application::createDocument()
{
    QPointer<Document> doc = Core::instance().documentContainer()->createDocument();
    Q_ASSERT(doc);
    Q_ASSERT(doc->undoStack());
    m_undoGroup->addStack(doc->undoStack());
    emit documentCreated(doc);
    setActiveDocument(doc);
    return doc;
}

void Application::setActiveDocument(QPointer<Document> doc)
{
    if(activeDocument() == doc) return;
    Core::instance().documentContainer()->setActiveDocument(doc);
    m_undoGroup->setActiveStack(doc->undoStack());
    emit activeDocumentChanged(doc);
}

void Application::removeDocument(QPointer<Document> doc)
{
    Core::instance().documentContainer()->removeDocument(doc);
    emit documentRemoved(doc);
}

QPointer<Document> Application::activeDocument() const
{
    return Core::instance().documentContainer()->activeDocument();
}

const DocumentList &Application::documents() const
{
    return Core::instance().documentContainer()->documents();
}

QPointer<StateManager> Application::toolsStateManager() const
{
    return m_toolsStateManager;
}

QPointer<QUndoGroup> Application::undoGroup() const
{
    return m_undoGroup;
}

void Application::initTools()
{
    m_toolManager = new ToolManager(this);
    m_toolsStateManager = new StateManager(this);

    connect(m_toolManager, SIGNAL(toolCategoryAdded(QPointer<ToolCategory>)),
        m_mainWindow, SLOT(onToolCategoryAdded(QPointer<ToolCategory>)));

    connect(m_toolManager, SIGNAL(toolAdded(QPointer<Tool>)),
        m_mainWindow, SLOT(onToolAdded(QPointer<Tool>)));

    connect(m_mainWindow, SIGNAL(toolChanged(QPointer<State>)),
            m_toolsStateManager, SLOT(setCurrentState(QPointer<State>)));

    registerTools();
}

void Application::registerTools()
{
    m_toolManager->registerCategory(new ToolCategory("Transform", "resources\\icons\\transformCategory.png", this));
    m_toolManager->registerCategory(new ToolCategory("Create", "resources\\icons\\createCategory.png", this));

    m_toolManager->registerTool(new ToolSelect(m_toolManager));
    m_toolManager->registerTool(new ToolMove(m_toolManager));
    m_toolManager->registerTool(new ToolRotate(m_toolManager));
    m_toolManager->registerTool(new ToolCreateLine(m_toolManager));
}
